const query = require('../config/db.config.js')

const shopMod = {
  relevetGoodsMod: async (data) => {
    let res = []
    let goodsarr = []
    // 将商品id 取出依次查询
    for (let i = 0; i < data.length; i++) {
      goodsarr.push(data[i].split(','))
    }
    // 双重循环查询并合并
    for (let i = 0; i < goodsarr.length; i++) {
      let arr = []
      for (let j = 0; j < goodsarr[i].length; j++) {
        const temp = await query(`
        SELECT
         *
        FROM
          goodinfo
        WHERE
          gID  = (${goodsarr[i][j]})
       `)
        arr.push(...temp)
      }
      res.push(arr)
    }
    // console.log(res);
    return res
  },
  orderlistWebMod: async (data) => {
    const { way, uname, status, limit, page } = data
    let pageCurr = (page - 1) * limit
    console.log(page);
    let occ = ''
    // console.log(uname);
    let sql = `
    SELECT 
	de.good_nums,
	de.gIDs,
	de.way,
	de.total, 
	de.status, 
	de.appoi_time , 
	de.uname , 
	de.id , 
	us.home ,
	us.phone 
  FROM deliverinfo de , user us 
  WHERE us.uID = de.uID 
  AND  de.status  LIKE '%${status || occ}%'
  AND  de.uname LIKE '%${uname || occ}%'
  AND  de.way LIKE '%${way || occ}%'
  ORDER BY de.appoi_time desc
  LIMIT ${pageCurr},${limit}
  `
  console.log(sql);
    const res = await query(sql)
    let sql2 = `SELECT COUNT(*) total FROM deliverinfo ;`
    const total = await query(sql2)
    return {
      total: total[0].total,
      list: res
    }
  },
  orderlistMod: async (data) => {
    let {
      uID
    } = data
    let sql1 =
      `
      SELECT
       *
      FROM
        deliverinfo
      WHERE
        uID = "${uID}"
        order by appoi_time desc
     `
    const res = await query(sql1)
    return res
  },
  goodsUNIlistServ:async ()=>{
    let sql = `select * from goodinfo where status = 1`
    const res = await query(sql)
    return res
  },
  goodslistMod: async (data) => {
    const { good_store, good_price, good_name, limit, page,status } = data
    let pageCurr = (page - 1) * limit
    let occ = ''
    let sql =
      `
      SELECT
       *
      FROM
        goodinfo gd
        WHERE 
         (gd.good_price="${ good_price || occ }" or "${ good_price || occ }"='') 
        AND (gd.good_store="${ good_store || occ }" or "${ good_store || occ }"='') 
        AND  gd.good_name LIKE '%${good_name || occ}%'
        AND (gd.status="${ status || occ }" or "${ status || occ }"='') 
        ORDER BY time desc
        LIMIT ${pageCurr},${limit}
        `
    const res = await query(sql)

    let sql2 = `SELECT COUNT(*) total FROM goodinfo ;`
    const total = await query(sql2)

    return {
      total: total[0].total,
      list: res
    }
  },
  deliverInfoMod: async (data) => {
    let {
      gIDs,
      good_nums,
      uID,
      uname,
      total,
      way,
      appoi_time
    } = data
    let gIDsStr = gIDs.join(',')
    // console.log(gIDsStr);
    let good_numsStr = good_nums.join(',')
    let sql =
      `
    INSERT deliverinfo 
    (gIDs ,good_nums ,uID , total, way ,appoi_time,uname )
     VALUES ( "${gIDsStr}" ," ${good_numsStr}" ,"${uID}" , ${total}, "${way}" ,"${appoi_time}","${uname}")
   `
    await query(sql)
    let sql1 = `UPDATE appeal set uname = "${uname}" WHERE uID = "${uID}"`
    await query(sql1)
  },
  deleteorderMod: async (data) => {
    const {
      id
    } = data
    let sql = `DELETE FROM deliverinfo  where  id = ${id}`
    await query(sql)
  },
  editOrderStatusWebMod: async (data) => {
    const { status, id } = data
    let sql =
      `UPDATE deliverinfo 
      SET
      status = "${status}"
      where id = "${id}"
    `
    await query(sql)
  },
  deleteGoodWebMod: async (data) => {
    const {
      gID
    } = data
    let sql = `DELETE FROM goodinfo  where  gID = ${gID}`
    await query(sql)
  },
  editGoodWebMod: async (data) => {
    const { status, gID, good_price , good_name,good_store,scale,img } = data
    let sql =
      `UPDATE goodinfo 
      SET
      status = "${status}",
      good_price = "${good_price}",
      good_name = "${good_name}",
      good_store = "${good_store}",
      scale = "(${scale})",
      img = "${img}"
      where gID = "${gID}"
    `
    await query(sql)
  },
  addGoodWebMod: async (data) => {
    const { status, good_price , good_name,good_store,scale,img } = data
    let sql =
    `INSERT INTO goodinfo 
    (status,good_price,good_name,good_store,scale,img) 
     VALUES 
    ("${status}", "${good_price}", "${good_name}","${good_store}", "${scale}", "${img}")`
    await query(sql)
  }
}

module.exports = shopMod